﻿using System;
using KPIS.DBM;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;

using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MAS_BankAccountHistories : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_MAS_BankAccounts bankAcc = new B_FIM_MAS_BankAccounts();
        MailMerge mailMerge = new MailMerge();
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblBankAccSeq.Text = Request.QueryString["BankAccSeq"];
                grdHistory.DataSource = new DataTable();
                grdHistory.DataBind();
            }
        }

        protected void LoadData()
        {
            DateTime? dFromDate = txtFromDate.GetDateValue();
            DateTime? dToDate = txtToDate.GetDateValue();

            grdHistory.DataSource = bankAcc.GetBankAccountHistory(lblBankAccSeq.Text, dFromDate, dToDate);
            grdHistory.DataBind();
        }

        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void grdHistory_ItemCommand(object sender, GridCommandEventArgs e)
        {
            LoadData();
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            lblBankAccSeq.Text = Request.QueryString["BankAccSeq"];
            DateTime? dFromDate = txtFromDate.GetDateValue();
            DateTime? dToDate = txtToDate.GetDateValue();

            DataTable dtb = bankAcc.GetBankAccountReport(Convert.ToInt32(lblBankAccSeq.Text), dFromDate, dToDate);
            DataTable dtb2 = bankAcc.SumAmountReport(Convert.ToInt32(lblBankAccSeq.Text), dFromDate, dToDate);

            Dictionary<string, DataTable> dtbs = new Dictionary<string, DataTable>();
            Dictionary<string, string> dic = new Dictionary<string, string>();

            dtbs.Add("H4", dtb2);

            DataRow dr1 = dtb2.Rows[0];
            decimal? SumRemain = main.ConvertNullDecimal(dr1["remain_amount"]);

            int nRow = 5;
            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                DataRow dr = dtb.Rows[i];
                
                string strTranDate = dr["tran_date"].ToString();
                string strRefNo = dr["ref_no"].ToString();
                string strType = dr["type"].ToString();
                string strInAmount = dr["in_amount"].ToString();
                string strOutAmount = dr["out_amount"].ToString();
                string strChequeNo = dr["cheque_no"].ToString();
                string strEmpName = dr["emp_name"].ToString();

                if (main.ConvertNullDecimal(strInAmount) != 0)
                {
                    SumRemain = SumRemain + main.ConvertNullDecimal(strInAmount);
                    dic.Add("H" + nRow.ToString(), SumRemain.ToString());
                }
                else if (main.ConvertNullDecimal(strOutAmount) != 0)
                {
                    SumRemain = SumRemain - main.ConvertNullDecimal(strOutAmount);
                    dic.Add("H" + nRow.ToString(), SumRemain.ToString());
                }

                dic.Add("A" + nRow.ToString(), strTranDate);
                dic.Add("B" + nRow.ToString(), strRefNo);
                dic.Add("C" + nRow.ToString(), strType);
                dic.Add("D" + nRow.ToString(), "");
                dic.Add("E" + nRow.ToString(), strChequeNo);
                dic.Add("F" + nRow.ToString(), strInAmount);
                dic.Add("G" + nRow.ToString(), strOutAmount);
                dic.Add("I" + nRow.ToString(), strEmpName);

                nRow++;
            }


            mailMerge.MergeExcel(Response, Server.MapPath("~/Reports/Templates/รายการเดินบัญชี.xlsx"), string.Format("รายการเดินบัญชี{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")), dtbs, dic);
        }
    }
}
